package de.hwrBerlin.dataMining.view.vImage.figures;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;

public abstract class Figure {
    
    public int x = 0, y = 0;
    protected Color _foreground = null;
    protected int _lineWidth = 1;
    protected int _antialias = SWT.ON;
    
    
    
    public abstract void paintImage(GC pGc);
    
    
    
    public Image toImage() {
        Image image = new Image(null, getBounds().x + getBounds().width, getBounds().y + getBounds().height);
        GC gc = new GC(image);
        
        paintImage(gc);
        
        return image;
    }
    
    
    
    public abstract void paintSVG(Document pPdocument);
    
    
    
    public Document toSVG() {
        Document document = DocumentHelper.createDocument();
        paintSVG(document);
        return document;
    }
    
    
    
    protected void setValuesToGc(GC pGc) {
        if (_foreground != null) {
            pGc.setForeground(_foreground);
        }
        pGc.setAntialias(_antialias);
        pGc.setLineWidth(_lineWidth);
    }
    
    
    
    protected void setValuesToSVGElement(Element pElement) {
        String value = "";
        
        if (_foreground != null) {
            value += "stroke:#" + colorToHex(_foreground) + ";";
        }
        value += "stroke-width:" + _lineWidth + "px;";
        
        pElement.addAttribute("style", value);
    }
    
    
    
    protected String colorToHex(Color pColor) {
        String hex = "";
        int[] rgb = {pColor.getRed(), pColor.getGreen(), pColor.getBlue()};
        
        for(int i = 0; i < rgb.length; i++){
            String hexPart = Integer.toHexString(rgb[i]);
            if(hexPart.length() == 1){
                hexPart = "0" + hexPart;
            }
            hex += hexPart;
        }
        
        return hex;
    }



    public abstract Rectangle getBounds();
    
    
    
    public void setForeground(Color pColor) {
        _foreground = pColor;
    }
    
    
    
    public Color getForeground() {
        return _foreground;
    }
    
    
    
    public void setLineWidth(int pWidth) {
        _lineWidth = pWidth;
    }
    
    
    
    public int getLineWidth() {
        return _lineWidth;
    }
    
    
    
    public void setAntialias(int pAntialias) {
        _antialias = pAntialias;
    }
    
    
    
    public int getAntialias() {
        return _antialias;
    }
    
}
